package uestc.lj.gulishop.search.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uestc.lj.common.exception.ErrorCode;
import uestc.lj.common.to.SkuEsModel;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.search.service.ProductSaveService;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search/save")
@Slf4j
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 商品上架
     *
     * @param skuEsModels
     * @return
     */
    @PostMapping("/product")
    public R productStatusUP(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b = false;
        try {
            b = productSaveService.productStatusUP(skuEsModels);
        } catch (IOException e) {
            log.error(e.getMessage());
            return R.error(ErrorCode.PRODUCT_UP_EXCEPTION.getCode(), ErrorCode.PRODUCT_UP_EXCEPTION.getMessage());
        }
        return b ? R.error(ErrorCode.PRODUCT_UP_EXCEPTION.getCode(), ErrorCode.PRODUCT_UP_EXCEPTION.getMessage()) : R.ok();
    }
}
